package com.example.wm.SanWar.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.SanWar.VO.RosterMemberVo;
import com.example.wm.SanWar.entity.AlliesandUser;
import com.example.wm.SanWar.entity.Membershiproster;
import com.example.wm.SanWar.entity.RosterMemberList;
import com.example.wm.SanWar.mapper.RosterMemberListMapper;
import com.example.wm.SanWar.service.AlliesandUserService;
import com.example.wm.SanWar.service.RosterMemberListService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/RosterMemberListController")
public class RosterMemberListController {
    @Resource
    private RosterMemberListService rosterMemberListService;
    @Resource
    private RosterMemberListMapper rosterMemberListMapper;
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private AlliesandUserService alliesandUserService;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody RosterMemberList rosterMemberListList) {

        rosterMemberListList.setCreatetime(tsyUtils.getnowtime());
        Boolean b= rosterMemberListService.save(rosterMemberListList);
        if(b)
        {
            return ResultData.success(rosterMemberListList.getId());

        }else{
            return ResultData.error(202, String.valueOf(b));
        }

    }
    @Transactional
    @PostMapping("/deletebyid")
    public Result deletebyid(@RequestBody RosterMemberList rosterMemberListList) {
        Boolean b= rosterMemberListService.removeById(rosterMemberListList);
        if(b)
        {
            return ResultData.success(rosterMemberListList.getId());

        }else{
            return ResultData.error(202, String.valueOf(b));
        }

    }

    @Transactional
    @PostMapping("/deleteBantchbyid")
    public Result deleteBantchbyid(@RequestBody Membershiproster membershiproster) {
        LambdaQueryWrapper<RosterMemberList> lq=new LambdaQueryWrapper<>();
        lq.eq(RosterMemberList::getMcid,membershiproster.getId());
        List<RosterMemberList> lists=rosterMemberListService.list(lq);

        if(lists.size()>0){
          rosterMemberListService.removeBatchByIds(lists);
        }

        return  ResultData.success("成功删除");



    }
    @Transactional
    @PostMapping("/findbymid")
    public Result findbymid(@RequestBody RosterMemberList rosterMemberListList) {
        LambdaQueryWrapper<RosterMemberList> lq=new LambdaQueryWrapper<>();
        lq.eq(RosterMemberList::getMcid,rosterMemberListList.getMcid());
        List<RosterMemberList> lists= rosterMemberListService.list(lq);
        if(!lists.isEmpty())
        {
            return ResultData.success(lists);

        }else{
            return ResultData.error(202, "无数据");
        }

    }

    @Transactional
    @PostMapping("/searchbyname")
    public Result searchbyname(@RequestBody RosterMemberList rosterMemberListList) {

        LambdaQueryWrapper<RosterMemberList> lq=new LambdaQueryWrapper<>();
        lq.like(RosterMemberList::getName,rosterMemberListList.getName());
        lq.eq(RosterMemberList::getOpernameid,rosterMemberListList.getOpernameid());
        List<RosterMemberList> lists= rosterMemberListService.list(lq);
        if(!lists.isEmpty())
        {

                lists.forEach(r->{
                    LambdaQueryWrapper<AlliesandUser> lqa=new LambdaQueryWrapper<>();
                    lqa.eq(AlliesandUser::getId,r.getNameid());
                    AlliesandUser one = alliesandUserService.getOne(lqa);
                    if(!Objects.equals(one.getName(), r.getName())) {
                        r.setName(one.getName());
                        rosterMemberListMapper.updateById(r);
                    }
//                    LambdaQueryWrapper<AlliesandUser> lqa2=new LambdaQueryWrapper<>();
//                    lqa2.eq(AlliesandUser::getId,r.getOpernameid());
//                    AlliesandUser one2 = alliesandUserService.getOne(lqa2);
//                    if(!Objects.equals(one2.getName(), r.getOpername())) {
//                        r.setOpername(one2.getName());
//                    }
                });



            return ResultData.success(lists);

        }else{
            return ResultData.error(202, "无数据");
        }

    }



}
